<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备类型" prop="equipmentId">
			<el-select v-model="dataForm.equipmentId" placeholder="请选择">
			    <el-option
			      v-for="item in equipmentList"
			      :key="item.id"
			      :label="item.equipmentName"
			      :value="item.id">
			    </el-option>
			  </el-select>
    </el-form-item>
    <el-form-item label="站点编号" prop="cashSiteNo">
      <el-input v-model="dataForm.cashSiteNo" placeholder="站点编号"></el-input>
    </el-form-item>
    <el-form-item label="站点名称" prop="cashSiteName">
      <el-input v-model="dataForm.cashSiteName" placeholder="站点名称"></el-input>
    </el-form-item>
    <el-form-item label="收银设备ID" prop="equipmentCashId">
      <el-input v-model="dataForm.equipmentCashId" placeholder="收银设备ID"></el-input>
    </el-form-item>
    <!-- <el-form-item label="收银设备名字" prop="equipmentCashName">
      <el-input v-model="dataForm.equipmentCashName" placeholder="收银设备名字"></el-input>
    </el-form-item> -->
   <!-- <el-form-item label="收银区域" prop="cashArea">
      <el-input v-model="dataForm.cashArea" placeholder="收银区域"></el-input>
    </el-form-item> -->
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
				equipmentList:[],
        visible: false,
				disabledSubmit:false,
        dataForm: {
          id: 0,
          sellerId: '',
          equipmentId: '',
          cashSiteNo: '',
          cashSiteName: '',
          equipmentCashId: '',
          equipmentCashName: '',
          cashArea: '',
          createTime: '',
          remark: ''
        },
        dataRule: {
          sellerId: [
            { required: true, message: '租户不能为空', trigger: 'blur' }
          ],
          equipmentId: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          cashSiteNo: [
            { required: true, message: '站点编号不能为空', trigger: 'blur' }
          ],
          cashSiteName: [
            { required: true, message: '站点名称不能为空', trigger: 'blur' }
          ],
          equipmentCashId: [
            { required: true, message: '收银设备ID不能为空', trigger: 'blur' }
          ],
          equipmentCashName: [
            { required: true, message: '收银设备名字不能为空', trigger: 'blur' }
          ],
          cashArea: [
            { required: true, message: '收银区域不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
				this.$http({
				  url: this.$http.adornUrl('/store/tstoreequipment/list'),
				  method: 'get',
				  params: this.$http.adornParams({
				    'page': this.pageIndex,
				    'limit': this.pageSize,
				    'key': this.dataForm.key
				  })
				}).then(({data}) => {
				  if (data && data.code === 0) {
				    this.equipmentList = data.page.list
				    this.totalPage = data.page.totalCount
				  } else {
				    this.equipmentList = []
				    this.totalPage = 0
				  }
				})

        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorecashsite/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tStoreCashSite.sellerId
                this.dataForm.equipmentId = data.tStoreCashSite.equipmentId
                this.dataForm.cashSiteNo = data.tStoreCashSite.cashSiteNo
                this.dataForm.cashSiteName = data.tStoreCashSite.cashSiteName
                this.dataForm.equipmentCashId = data.tStoreCashSite.equipmentCashId
                this.dataForm.equipmentCashName = data.tStoreCashSite.equipmentCashName
                this.dataForm.cashArea = data.tStoreCashSite.cashArea
                this.dataForm.createTime = data.tStoreCashSite.createTime
                this.dataForm.remark = data.tStoreCashSite.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorecashsite/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'equipmentId': this.dataForm.equipmentId,
                'cashSiteNo': this.dataForm.cashSiteNo,
                'cashSiteName': this.dataForm.cashSiteName,
                'equipmentCashId': this.dataForm.equipmentCashId,
                'equipmentCashName': this.dataForm.equipmentCashName,
                'cashArea': this.dataForm.cashArea,
                'createTime': this.dataForm.createTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
          else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
